/**
 * Created by buzhizhe on 2019/2/10.
 */
var controllerName="/parenttree";
var VM=new Object();
var loading = false;
var loadall=false;


$(document).ready(function() {
    initDataTable();
    addEvents();
});

//加载事件
function addEvents() {
    //滚动加载事件
    $(document.body).infinite().on("infinite", function () {
        if (loadall) {
            $("#loadingok").show();
            $("#loading").hide();
            return;
        }
        if (loading) return;
        loading = true;
        $("#loading").show();
        var pageindex = parseInt($("#pageindex").val())+1;
        //$("#pageindex").val(pageindex);
        setTimeout(function () {
            getListData(pageindex);
            loading = false;
        }, 1000);
    });

    //行点击事件
    $(document).on("click", ".tbl-row", function() {
        var id=$(this)[0].attributes["data-id"].value;
        $.actions({
            title: "选择操作",
            onClose: function() {
                console.log("close");
            },
            actions: [
                {
                    text: "查看",
                    className: "color-warm",
                    onClick: function() {
                        popupView(id);
                    }
                },
                {
                    text: "编辑",
                    className: "color-primary",
                    onClick: function() {
                        popupEdit(id);
                        //$(location).attr('href', '/memberuser/add/'+id);
                    }
                },
                {
                    text: "删除",
                    className: 'color-danger',
                    onClick: function() {
                        doDelete(id);
                    }
                }
            ]
        });

    });

	/*
    //状态处理
    $("#txtshowstatus").select({
        title: "选择状态",
        items: arr_items["status"],
        onClose: function(d) {
            $("#txt-status").val(d.data.values);
            console.log(d.data.values);
        }
    });
	*/

}
//初始化VM
function initDataTable() {
    VM = new Vue({
        delimiters: ["[[", "]]"],
        el: '#app',
        data: {listdata: []}
    });
    $("#pageindex").val(0);
    getListData(1);
    //getMoreData();
}

//ajxa获取列表数据
function getListData(pageindex) {
    if (pageindex == undefined) pageindex = 1;
    $("#pageindex").val(pageindex);
    var url = domain + controllerName + "/index";
    var formData = getFormData($('#formQuery'));
    var ret = [];
    $.ajax({
        type: "POST",
        url: url,
        async: false,
        dataType: "json",
        data: { "formData": JSON.stringify(formData) },
        error: function (request) {
            ajaxError(request,$(this));
        },
        success: function (result) {
            //result = eval("(" + result + ")");
            ajaxSuccess(result, $(this));
            result.data.dataList.forEach(function (v) {
                //状态赋值
                //v.statusname=getTitleByValue(arr_items["status"],v.status);
                VM.listdata.push(v);
            });
            $("#loading").hide();
            //$.hideLoading();
            if (result.data.dataList.length < result.data.pageSize) { loadall = true; }

        }
    });
}

//打开表单
function popupEdit(id) {
    if(id==undefined)id=0;
    var url= domain + controllerName + "/get/"+id
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                var data = result.data;
                //data["showstatus"]=getTitleByValue(arr_items["status"],data["status"]);
                for (var k in data) {
                    //console.log(data[k]);
                    $("input[name='" + k + "']").val(data[k]);
                }
                $("#layer-edit").popup();
            } else {
                $.alert("获取失败：" + result.message);
            }
        }
    }
    );
}
//打开表单
function popupView(id) {
    if(id==undefined)id=0;
    var url= domain + controllerName + "/get/"+id
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                //data["statusname"]=getTitleByValue(arr_items["status"],data["status"]);
                var data = result.data;
                for (var k in data) {
                    //console.log(data[k]);
                    $("span[name='" + k + "']").html(data[k]);
                }
                $("#layer-view").popup();
            } else {
                $.alert("获取失败：" + result.message);
            }
        }
    }
    );
}

//查询
function formQuerySubmit() {
    VM.listdata.length=0;
    loadall = false;
    getListData(1);
    //$("#formQuery").submit();
}

//保存
function formEditSubmit() {
    var url = domain + controllerName + "/update";
    var formData = getFormData($('#formEdit'));
    $.ajax({
        type: "post",
        url: url,
        data: { "formData": JSON.stringify(formData) },
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code=result.statecode;
            if(code==0){
                $.closePopup("#layer-edit");
                $.alert("提交成功", "提示", function() {
                    //$("#formQuery")[0].reset();
                    var pageindex = parseInt($("#pageindex").val()) + 1;
                    getListData(pageindex);
                });
            }else{
                $.alert("提交失败："+result.message);
            }
        }
      }
    );

}
//删除
function doDelete(id) {
    var url= domain + controllerName + "/delete/"+id
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                $.alert("提交成功", "提示", function () {
                    formQuerySubmit();
                    //location.href=controllerName + "/list";
                    //$(location).attr('href', '/memberuser/list/');
                });
            } else {
                $.alert("提交失败：" + result.message);
            }
        }
    }
    );

}




